<template>
	<view class="">
		<view class="category-select">
			<view class="nav-wrap">
				<text class="color-base-border" :class="selectCateOne" @click="selectCate(1)">常规商品</text>
				<text class="color-base-border" :class="selectCateTwo" @click="selectCate(2)">营销商品</text>
			</view>
			<view class="search-wrap">
				<view class="search-input-inner">
					<text class="search-input-icon iconfont iconsousuo" @click="searchGoods()"></text>
					<input
						class="search-input-text font-size-tag"
						maxlength="50"
						v-model="keyword"
						placeholder="请输入商品名称"
						@confirm="searchGoods()"
					/>
				</view>
			</view>
		</view>
		
		<view class="category-level" :style="mainHeight">
			<view class="category-level-box" v-if="cateList.length && isLoadingCate">
				<view class="category-goods">
					<scroll-view scroll-y="true" class="oneScroll">
						<view
							class="oneScroll-item using-hidden"
							v-for="(item, index) in cateList"
							:key="index"
							:class="{ 'active color-base-text color-base-bg-before': index == oneIndex }"
							@click="selectOneCategory(index)"
						>
							{{ item.short_name ? item.short_name : item.category_name }}
						</view>
					</scroll-view>
					<view class="twoScroll">
						<view class="category-cate-top">
							<scroll-view scroll-x="true" class="three-two-cate" v-if="twoCateList.length">
								<text
									class="three-two-cate-item using-hidden"
									:class="twoCateSelected == -1 || twoCateSelected == 1 ? 'active color-base-border color-base-text' : ''"
									@click="twoCateSelected = 1"
								>
									全部
								</text>
								<text
									class="three-two-cate-item using-hidden"
									:class="index == TwoCategoryIndex && !twoCateSelected ? 'active color-base-border color-base-text' : ''"
									v-for="(item, index) in twoCateList"
									:key="index"
									@click="selectTwoCategory(item.category_id_2, index)"
								>
									{{ item.category_name }}
								</text>
							</scroll-view>
						</view>
						<template v-show="!isLoading" v-if="index == 2">
							<scroll-view scroll-y="true" :style="{ height: 'calc(100%)' }" @scrolltolower="getGoodsList()">								
								<block v-for="(item, index) in goodsList" :key="index">
									<view class="item-active">
										<view class="item-select-info">
											<view class="item-title goods_name multi-hidden">
												{{item.pintuan_name}}
											</view>
											<view v-if="item.is_check == -1" class="item-but color-disabled-bg">不可选</view>
											<view v-if="item.is_check == 0" @click.stop="selectGoods(index,item.market_id,item.category)" class="item-but color-base-bg">入库</view>
											<view v-if="item.is_check == 1" class="item-but color-disabled-bg">已选</view>
										</view>
										<view class="twoScroll-item-box" @click="toDetail(item.sku_id)">
											<image class="twoScroll-item-image" :src="$util.img(item.sku_image, { size: 'mid' })"mode="aspectFill" />
											<view class="goods_detail">
												<view class="goods_name multi-hidden">{{ item.goods_name }}</view>
												<view class="goods_price">
													<view class="goods-price-box">
														<text class="sale-price">
															<text class="price-icon">售价￥</text>
															{{ item.price }}
														</text>
													</view>
													<view class="goods-price-box">
														<text class="sale-price color-base-text">
															<text class="price-icon">利润￥</text>
															{{ item.profit }}
														</text>
													</view>
												</view>
											</view>
										</view>
									</view>
								</block>
		
								<!-- 加载中 -->
								<view class="mescroll-upwarp" v-show="false"><ns-loading></ns-loading></view>
								<ns-empty v-if="isAll && goodsList.length == 0" :isIndex="!1" :fixed="!1" text="暂无活动"></ns-empty>
							</scroll-view>
						</template>
						<template v-show="!isLoading" v-else>
							<scroll-view scroll-y="true" :style="{ height: 'calc(100%)' }" @scrolltolower="getGoodsList()">							
								<block v-for="(item, index) in goodsList" :key="index">
									<view class="twoScroll-item-box" @click="toDetail(item.sku_id)">
										<image class="twoScroll-item-image" :src="$util.img(item.sku_image, { size: 'mid' })" mode="aspectFill" />
										<view class="goods_detail">
											<view class="goods_name multi-hidden">{{ item.goods_name }}</view>
											<view class="goods_price">
												<view class="goods-price-box">
													<text class="sale-price">
														<text class="price-icon">售价￥</text>
														{{ item.price }}
													</text>
												</view>
												<view class="goods-price-box">
													<text class="sale-price color-base-text">
														<text class="price-icon">利润￥</text>
														{{ item.profit }}
													</text>
												</view>
												<view class="careBox_more" @click.stop>
													<text v-if="item.is_check == -1" class="color-disabled-bg">不可选</text>
													<text v-if="item.is_check == 0" @click.stop="selectGoods(index,item.market_id,item.category)" class="color-base-bg">入库</text>
													<text v-if="item.is_check == 1" class="color-disabled-bg">已选</text>
												</view>
											</view>
										</view>
									</view>
								</block>
								
								<!-- 加载中 -->
								<view class="mescroll-upwarp" v-show="false"><ns-loading></ns-loading></view>
								<ns-empty v-if="isAll && goodsList.length == 0" :isIndex="!1" :fixed="!1" text="该分类下暂无商品"></ns-empty>
							</scroll-view>
						</template>
					</view>
				</view>
			</view>
			<ns-empty :isIndex="!1" v-else></ns-empty>
		
			<!-- 加载中 -->
			<view class="mescroll-upwarp" v-show="!isLoadingCate"><ns-loading></ns-loading></view>
		</view>
	</view>
</template>

<script>
import nsLoading from '@/components/ns-loading/ns-loading.vue';
import globalConfig from '@/common/js/golbalConfig.js';
export default {
	components: {
		nsLoading
	},	
	data() {
		return {
			index: 1,
			selectCateOne :'selected color-base-bg',
			selectCateTwo: '',
			siteId: 1,
			oneIndex: 0,
			goodsList: [],
			cateList: [], //分类
			oneCategoryId: 0, //当前分类菜单的id
			categoryAdvImage: '',
			isLoadingCate: false,
			isLoading: false,
			isNetwork: 1,
			isAll: 0,
			page_num: 1,
			page_size: 10,
			keyword: '',
			is_select: false,
			windowHeight: 0,
			twoCateList: [],
			TwoCategoryIndex: 0,
			twoCateSelected: 1
		};
	},
	computed: {
		mainHeight() {
			var height = this.windowHeight ? this.windowHeight + 'px' : '100%';
			height = 'height:' + height;
			return height;
		}
	},
	watch: {
		twoCateSelected: function(newQuestion, oldQuestion) {
			if (newQuestion == 1) {
				this.TwoCategoryId = 0;
				this.page_num = 1;
				this.isAll = false;
				this.isNetwork = true;
				this.goodsList = [];
				this.getGoodsList();
			}
		}
	},
	mixins: [globalConfig],
	onLoad() {
		this.getCategoryList();
		this.getHeight();
	},
	methods: {
		getHeight() {
			this.height = 0;
			var self = this;
			uni.getSystemInfo({
				success: function(res) {
					var minusHeight = 0;
					self.$nextTick(() => {
						const query = uni.createSelectorQuery().in(self);
						
						query
							.select('.category-select')
							.boundingClientRect(data => {
								minusHeight += data.height;
								minusHeight += 30;
								self.windowHeight = res.windowHeight - minusHeight;
							})
							.exec();
					});
				}
			});
			
		},
		selectCate(i) {
			this.index = i;
			if (i == 2) {
				this.selectCateTwo = 'selected color-base-bg';
				this.selectCateOne = '';
			} else {
				this.selectCateOne = 'selected color-base-bg';
				this.selectCateTwo = '';
			}
			this.oneIndex = 0;
			this.page_num = 1;
			this.isAll = false;
			this.isNetwork = true;
			this.goodsList = [];
			this.getCategoryList();
		},
		searchGoods() {
			this.page_num = 1;
			this.isAll = false;
			this.isNetwork = true;
			this.goodsList = [];
			this.getCategoryList();
		},
		selectGoods(index,market_id,category) {
			if (this.is_select) {
				return;
			}
			this.is_select = true;
			this.$api.sendRequest({
				url: '/market/api/goods/createMarket',
				data: {market_id,category},
				success: res => {
					this.is_select = false;
					this.$util.showToast({
						title: res.message
					});
					if (res.code == 0) {
						this.goodsList[index]['is_check'] = 1;
					}
				}
			});
		},
		getCategoryList() {
			if (this.index == 1) {
				let url = '/market/api/goods/goodsCategory';
				this.isLoadingCate = false;
				this.$api.sendRequest({
					url: url,
					data: {},
					success: res => {
						this.isLoadingCate = true;
						if (res.code != 0) {
							this.$util.showToast({
								title: res.message
							});
							return false;
						}
						if (res.code == 0 && res.data.length) {
							this.cateList = res.data;
							this.twoCateList = res.data[0].child_list;
							this.getGoodsList();
						}
					}
				});
			} else {
				this.isLoadingCate = true;
				this.twoCateList = [];
				this.cateList = [
					{
						category_id_1: 1,
						category_name: '拼团'
					},
					{
						category_id_1: 2,
						category_name: '秒杀'
					},
					{
						category_id_1: 3,
						category_name: '砍价'
					},
					{
						category_id_1: 4,
						category_name: '团购'
					}
				];
				this.getGoodsList();
			}
		},
		showPrice(data) {
			let price = data.discount_price;
			if (data.member_price && data.member_price < price) price = data.member_price;
			return price;
		},
		getGoodsList() {
			if (!this.isNetwork || this.isAll) return;

			this.isNetwork = 0;
			this.isLoading = true;

			let data = {
				page: this.page_num,
				page_size: this.page_size
			};			
			if (this.TwoCategoryId > 0) {
				data.category_id = this.TwoCategoryId;
			} else {
				data.category_id = this.cateList[this.oneIndex].category_id_1;
			}
			if (this.keyword) {
				data.keyword = this.keyword;
			}
				
			var url = '/market/api/goods/getGoods';
			if (this.index == 2) {
				url = '/market/api/goods/marketList';
			}
			this.$api.sendRequest({
				url: url,
				data: data,
				success: res => {
					this.isNetwork = 1;
					this.isLoading = false;

					if (res.code == 0 && res.data) {
						let newArr = res.data.list.length ? res.data.list : [];
						for (let i in newArr) {
							console.log(newArr[i]['goods_image'])
							newArr[i]['sku_image'] = newArr[i]['goods_image'].split(',')[0];
						}
						console.log(newArr)
						//设置列表数据
						if (this.page_num == 1) this.goodsList = []; //如果是第一页需手动制空列表
						this.goodsList = this.goodsList.concat(newArr); //追加新数1据
						++this.page_num;

						if (this.goodsList.length == res.data.count) this.isAll = true;

					}

				}
			});
		},
		selectOneCategory(index) {
			this.oneIndex = index;
			this.TwoCategoryId = 0;
			if (this.index == 1) {
				this.twoCateList = this.cateList[this.oneIndex].child_list;
			}

			this.page_num = 1;
			this.isAll = false;
			this.isNetwork = true;
			this.goodsList = [];
			this.getGoodsList();			
		},
		selectTwoCategory(twoCateId, index) {
			this.twoCateSelected = 0;
			this.TwoCategoryId = twoCateId;
			this.TwoCategoryIndex = index;
		
			this.page_num = 1;
			this.isAll = false;
			this.isNetwork = true;
			this.goodsList = [];
			this.getGoodsList();
		}
	}
};
</script>
<style lang="scss">
@import './market.scss';
/* 上拉加载区域 */
.mescroll-upwarp {
	margin-top: 35vh;
}
.nav-wrap {
	background-color: #fff;
	text-align: center;
	padding: 30rpx 0;
	text {
		border: 1px solid;
		height: 70rpx;
		line-height: 70rpx;
		padding: 0 40rpx;
		display: inline-block;
		border-radius: 40rpx;
		&.selected {
			color: #fff;
		}
		&:first-child {
			border-top-right-radius: 0;
			border-bottom-right-radius: 0;
		}
		&:last-child {
			border-top-left-radius: 0;
			border-bottom-left-radius: 0;
		}
	}
}
.search-wrap {
	display: flex;
	justify-content: space-between;
	background-color: #fff;
	margin: $margin-updown $margin-both;
	border-radius: 40rpx;
	.search-input-inner {
		display: flex;
		align-items: center;
		width: 460rpx;
		height: 70rpx;
		padding: 0 30rpx;
		border-radius: 100rpx;
		box-sizing: border-box;
		.search-input-icon {
			margin-right: 10rpx;
			color: $color-tip;
		}
	}
}
.item-active {
	padding: 10rpx;
	border: 1rpx solid #ebebeb;
	margin-bottom: 20rpx;
}

.item-select-info {
	display: flex;
	justify-content: space-between;
	align-items: flex-start;
}
.item-title {
	width: 450rpx;
	color: #333333;
	font-weight: 600;
	font-size: 28rpx;
	margin-bottom: 15rpx;
}
.item-but {
	display: inline-block;
	width: 66px;
	height: 27px;
	line-height: 18px;
	text-align: center;
	color: #fff;
	padding: 4px;
	border-radius: 24px;
	box-sizing: border-box;
	font-size: 13px;
}
.three-two-cate {
	width: 100%;
	padding-bottom: 30rpx;
	box-sizing: border-box;
	line-height: 1;
	white-space: nowrap;

	.three-two-cate-item {
		display: inline-block;
		height: 50rpx;
		line-height: 36rpx;
		margin-right: 20rpx;
		padding: 6rpx 16rpx;
		border: 1px solid $color-disabled;
		box-sizing: border-box;
		border-radius: 45rpx;
		color: $color-title;
		text-align: center;
		font-size: $font-size-tag;
	}
	.three-two-cate-item.active {
		border: 1px solid;
	}
}
</style>
<style scoped>
/deep/ .reward-popup .uni-popup__wrapper-box {
	background: none !important;
	max-width: unset !important;
	max-height: unset !important;
}
</style>
